package doantotnghiep.daoimpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import doantotnghiep.dao.CabinetDao;
import doantotnghiep.model.Cabinet;

public class CabinetDaoImpl implements CabinetDao{

	@Autowired
	private HibernateTemplate hibernateTemplate;
	@Override
	public void addCabinet(Cabinet cabinet) {
		this.hibernateTemplate.saveOrUpdate(cabinet);
	}

	@Override
	public void deleteCabinet(int id) {
		Cabinet cabinet = this.getCabinetByID(id);
		if(cabinet!=null){
			cabinet.setStatus(1);
			hibernateTemplate.saveOrUpdate(cabinet);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Cabinet> getListCabinet() {
		return hibernateTemplate.find("from Cabinet  where statusActivity = 0 order by id");
	}

	@SuppressWarnings("unchecked")
	@Override
	public Cabinet getCabinetByID(int id) {
		List<Cabinet> cabinets = hibernateTemplate.find("from Cabinet where statusActivity = 0 and id ="+ id);
		if(cabinets!=null){
			return cabinets.get(0);
		}
		return null;
	}

	@Override
	public void saveCabinet(Cabinet cabinet) {
		this.hibernateTemplate.saveOrUpdate(cabinet);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Cabinet> listNotFullCabinet() {
		return hibernateTemplate.find("from Cabinet where  statusActivity = 0 and status = 0");
	}

}
